package cn.qzc.controller;

import cn.qzc.annotation.LoginRequired;
import cn.qzc.entiy.User;
import cn.qzc.service.UserService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by HP on 2019/5/8.
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    private static final String CODE_OK = "200";
    private static final String CODE_NOTFOUND = "404";
    private static final String CODE_ERROR = "500";
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Object login(@RequestBody User user) {       //该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容，例如application/json, application/xml等；
        JSONObject jsonObject = new JSONObject();
        User userInDataBase = userService.findByName(user.getName());
        if (userInDataBase == null) {
            jsonObject.put("error", "用户不存在！");
        }else if(!userService.comparePassword(user,userInDataBase)){
            jsonObject.put("error", "密码不正确！");
        }else{
            String token = userService.getToken(userInDataBase);  //将对象存入token
            jsonObject.put("code",CODE_OK);
            jsonObject.put("token", token);
            jsonObject.put("user", userInDataBase);
            jsonObject.put("msg", "登陆成功！");
        }
        return jsonObject;
    }

    @PostMapping("/add")
    public Object addUser(@RequestBody User user) {
        JSONObject jsonObject = new JSONObject();
        User userInDataBase =userService.findByName(user.getName());
        if(userInDataBase!=null){
            jsonObject.put("error","用户名已经被使用！");
        }else{
            userService.addUser(user);
            jsonObject.put("succes","添加成功！");
            userInDataBase =userService.findByName(user.getName());
            jsonObject.put("user",userInDataBase);
        }
        return jsonObject;
    }

    @PostMapping("/del")
    @LoginRequired
    public Object delUser(@RequestParam("id") Integer id){//用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容，提交方式GET、POST；该注解有两个属性： value、required； value用来指定要传入值的id名称，required用来指示参数是否必须绑定；
        JSONObject jsonObject = new JSONObject();
        int result = userService.delUser(id);
        if(result==1)
            jsonObject.put("succes","删除成功！");
        else
            jsonObject.put("error","删除失败！");
        return jsonObject;
    }

    @PostMapping("/del2/{id}")
    public Object delUser2(@PathVariable String id){
        JSONObject jsonObject = new JSONObject();
        int result = userService.delUser(Integer.valueOf(id));
        if(result==1)
            jsonObject.put("succes","删除成功！");
        else
            jsonObject.put("error","删除失败！");
        return jsonObject;
    }
    @PostMapping("/update")
    public Object updateUser(@RequestBody User user){
        JSONObject jsonObject = new JSONObject();
        User updateAfterUser = userService.updateUser(user);
        jsonObject.put("user",updateAfterUser);
        return jsonObject;
    }
}
